package com.shujia.flink.sql

import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
import org.apache.flink.table.api.EnvironmentSettings
import org.apache.flink.table.api.bridge.scala.StreamTableEnvironment

object Demo6Hbase {
  def main(args: Array[String]): Unit = {


    //创建flink的环境
    val bsEnv: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    val bsSettings: EnvironmentSettings = EnvironmentSettings
      .newInstance()
      .useBlinkPlanner() //使用blink计划器
      .inStreamingMode() //流模式
      .build()

    /**
      * 创建flink sql 环境
      *
      */

    val bsTableEnv: StreamTableEnvironment = StreamTableEnvironment.create(bsEnv, bsSettings)




    bsTableEnv.executeSql(
      """
        |
        |CREATE TABLE student (
        | id STRING,
        | name STRING,
        | age INT,
        | gender STRING,
        | clazz STRING
        |)  WITH (
        |  'connector' = 'filesystem',
        |  'path' = 'flink/data/students.json',
        |  'format' = 'json',
        |  'json.ignore-parse-errors' = 'true'
        |)
        |
      """.stripMargin)

    bsTableEnv.executeSql(
      """
        |
        |CREATE TABLE hbase_student (
        | id STRING,
        | info ROW<name STRING , age INT , gender STRING , clazz STRING>,
        | PRIMARY KEY (id) NOT ENFORCED
        |) WITH (
        | 'connector' = 'hbase-1.4',
        | 'table-name' = 'student',
        | 'zookeeper.quorum' = 'master:2181'
        |)
        |
        |
      """.stripMargin)

    /**
      * 将数据写入hbase
      *
      */
    bsTableEnv.executeSql(
      """
        |
        |insert into hbase_student
        |select id , ROW(name,age,gender,clazz) as info from student
        |
      """.stripMargin)

  }

}
